package com.active.hackson.database.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.active.hackson.database.bean.Column;
import com.active.hackson.database.bean.PrimaryKey;
import com.active.hackson.database.bean.Table;

public class Converter {
    public static List< Table > convertTables( ResultSet rs ) throws SQLException {
        if ( rs != null ) {
            List< Table > tables = new ArrayList< Table >();
            while ( rs.next() ) {
                Table table = new Table();
                table.setName( rs.getString( "TABLE_NAME" ) );
                tables.add( table );
            }
            return tables;
        }
        return null;
    }

    public static List< PrimaryKey > convertPrimaryKeys( ResultSet rs ) throws SQLException {
        if ( rs != null ) {
            List< PrimaryKey > primaryKeys = new ArrayList< PrimaryKey >();
            while ( rs.next() ) {
                PrimaryKey primaryKey = new PrimaryKey();
                primaryKey.setColumnName( rs.getString( "COLUMN_NAME" ) );
                primaryKeys.add( primaryKey );
            }
            return primaryKeys;
        }
        return null;
    }

    public static List< Column > convertColumns( ResultSet rs ) throws SQLException {
        if ( rs != null ) {
            List< Column > columns = new ArrayList< Column >();
            while ( rs.next() ) {
                Column column = new Column();
                column.setName( rs.getString( "COLUMN_NAME" ) );
                column.setSize( rs.getInt( "COLUMN_SIZE" ) );
                column.setType( rs.getInt( "DATA_TYPE" ) );
                column.setNullable( rs.getBoolean( "IS_NULLABLE" ) );
                columns.add( column );
            }
            return columns;
        }
        return null;
    }
}
